package com.limbo.showapp.rxHttp;

import com.limbo.showapp.MyApplication;
import com.limbo.showapp.base.BaseBean;
import com.limbo.showapp.util.MyUtils;
import com.limbo.showapp.util.ToastUtils;

import rx.Observer;


/**
 * Created by wangqi on 2018/8/24.
 */

public abstract class HttpObserver<T> implements Observer<T> {

    protected abstract void onHttpError(Throwable e);

    protected abstract void onHttpSuccess(T bean);

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
        onHttpError(e);
//        ToastUtils.getInstanc(MyApplication.instance).showToast(e.getMessage());
    }

    @Override
    public void onNext(T t) {
        if (t instanceof BaseBean) {
            if (((BaseBean) t).getCode() == 200)
                onHttpSuccess(t);
            else{
                ToastUtils.getInstanc(MyApplication.instance).showToast(((BaseBean) t).getMessage());
                onHttpError(new Exception(((BaseBean) t).getMessage()));
            }
        } else {
            onHttpError(new Exception("数据转换发生异常"));
        }
    }
}
